package PaChong;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 13:58:52 2018/10/31
 * @version:2.0
 */
public class Product {
    public int imgNum;
    public String productName;
    public String productUrl;
    public String imgIndex;
    public String beizhu;
    public Double[] productSize;
    public List<ProductImg> productImgs;
    public boolean isSuccess = false;

    public static List<Product> getAllProducts(Category category) {
        List<Product> products = new ArrayList();
        String html = null;
        try {
            html = Html.getHtml(category.categoryUrl);
        } catch (IOException e) {
            category.isSuccess = false;
            // System.out.println("获取种类下产品失败" + category.categoryUrl);
            return null;

        }
        Document doc = Jsoup.parse(html);
        Elements selectedDoms = doc.select("a.album__main");//"form.pagination__jumpwrap"
        Iterator<Element> doms = selectedDoms.iterator();
        while (doms.hasNext()) {
            Element selectedDomA = doms.next();
            Product product = new Product();
            product.productName = selectedDomA.attr("title");
            product.productUrl = Category.pageUrl + selectedDomA.attr("href");

            Elements selectedDomImgs = selectedDomA.select("img[data-type='photo']");
            Iterator<Element> domImgs = selectedDomImgs.iterator();
            while (domImgs.hasNext()) {
                Element domImg = domImgs.next();
                product.imgIndex = domImg.attr("src");
            }
            products.add(product);
        }
        if (products.size() == 0) {
            category.isSuccess = true;
        }
        category.products = products;
        return products;
    }
}
